package com.wodsy.phoenix.datadao.shopc.dao.impl;

import com.wodsy.phoenix.datadao.base.BaseHibernateDao;
import com.wodsy.phoenix.datadao.shopc.dao.ShopkeeperAccountDao;
import com.wodsy.phoenix.dataobject.shopc.model.ShopkeeperAccount;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository("shopkeeperAccountDao")
public class ShopkeeperAccountDaoImpl extends BaseHibernateDao<ShopkeeperAccount, Integer> implements ShopkeeperAccountDao {
	@Autowired
	@Qualifier(value="shopc")
	@Override
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Class<ShopkeeperAccount> getModelClass() {
		return ShopkeeperAccount.class;
	}
	
	@Override
	public void delShopkeeperAccount(Integer accountId){
		String hql = "update ShopkeeperAccount acc set acc.delete = :delete,acc.useStatus = :useStatus,acc.updateTime = :updateTime where acc.accountId = :accountId";
		Query query = getSession().createQuery(hql);
		query.setBoolean("delete", true);
		query.setBoolean("useStatus", false);
		query.setTimestamp("updateTime", new Date());
		query.setInteger("accountId", accountId);
		query.executeUpdate();
	}
	
	@Override
	public void disableShopkeeperAccount(Integer accountId){
		String hql = "update ShopkeeperAccount acc set acc.useStatus = :useStatus,acc.updateTime = :updateTime where acc.accountId = :accountId";
		Query query = getSession().createQuery(hql);
		query.setBoolean("useStatus", false);
		query.setTimestamp("updateTime", new Date());
		query.setInteger("accountId", accountId);
		query.executeUpdate();
	}
	
	@Override
	public void enableShopkeeperAccount(Integer accountId){
		String disableAllHql = "update tb_shopkeeper_account acc set acc.use_status = :useStatus,acc.update_time = :updateTime where acc.shop_id = (select temp.shop_id from (select shop_id from tb_shopkeeper_account ac where account_id = :accountId) temp)";
		Query disableAll = getSession().createSQLQuery(disableAllHql);
		disableAll.setBoolean("useStatus", false);
		disableAll.setTimestamp("updateTime", new Date());
		disableAll.setInteger("accountId", accountId);
		disableAll.executeUpdate();
		
		String hql = "update ShopkeeperAccount acc set acc.useStatus = :useStatus,acc.updateTime = :updateTime where acc.accountId = :accountId";
		Query query = getSession().createQuery(hql);
		query.setBoolean("useStatus", true);
		query.setTimestamp("updateTime", new Date());
		query.setInteger("accountId", accountId);
		query.executeUpdate();
	}

}
